import launch
import launch_ros
from ament_index_python.packages import get_package_share_directory
import os
from launch.launch_description_sources import PythonLaunchDescriptionSource

def generate_launch_description():
    #获取所需的路径
    robot1_navigation2_dir=get_package_share_directory('robot1_navigation2')
    nav2_bringup_dir=get_package_share_directory('nav2_bringup')
    rviz_config_dir=os.path.join(nav2_bringup_dir,'rviz','nav2_default_view.rviz')

    #创建launch配置
    use_sim_time=launch.substitutions.LaunchConfiguration(
        'use_sim_time',default='True'
    )
    map_path=launch.substitutions.LaunchConfiguration(
        'map',default=os.path.join(robot1_navigation2_dir,'maps','room.yaml')
    )
    params_path=launch.substitutions.LaunchConfiguration(
        'params_file',default=os.path.join(robot1_navigation2_dir,'config','nav2_params.yaml')
    )

    return launch.LaunchDescription([
        #声明启动参数
        launch.actions.DeclareLaunchArgument('use_sim_time',default_value=use_sim_time,description='use gazebo simulation clock if try'),
        launch.actions.DeclareLaunchArgument('map',default_value=map_path,description='Full of map path to load'),
        launch.actions.DeclareLaunchArgument('params_file',default_value=params_path,description='Full of params path to load'),
        #用当前的launch文件启动示例启动文件
        launch.actions.IncludeLaunchDescription(
            PythonLaunchDescriptionSource(
                [nav2_bringup_dir,'/launch','/bringup_launch.py']
            ),
            launch_arguments={
                'map':map_path,
                'use_sim_time':use_sim_time,
                'params_file':params_path,}.items(),
        ),
        #启动rviz2并且传入参数
        launch_ros.actions.Node(
            package='rviz2',
            executable='rviz2',
            name='rviz2',
            arguments=['-d',rviz_config_dir],
            parameters=[{'use_sim_time': use_sim_time}],
            output='screen',
        )])